package asia.doing.controller.user;

import asia.doing.common.Result;
import asia.doing.entity.User;
import asia.doing.service.user.FrontUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController(value = "frontUserController")
@RequestMapping("/front/user")
public class FrontUserController {
    @Resource
    private FrontUserService frontUserService;

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        frontUserService.add(user);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        frontUserService.deleteById(id);
        return Result.success();
    }

    @PostMapping("delete/batch")
    public Result deleteBatch(@RequestBody List<Long> ids){
        frontUserService.deleteBatch(ids);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody User user){
        frontUserService.updateById(user);
        return Result.success();
    }


    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id){
        User user = frontUserService.selectById(id);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll(@RequestBody User user){
        List<User> list = frontUserService.selectAll(user);
        return Result.success();
    }

    @GetMapping("/selectPage")
    public Result selectPage(User user,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "2") Integer pageSize)
    {

        PageInfo<User> page = frontUserService.selectPage(user,pageNum,pageSize);
        return Result.success(page);
    }
}
